package com.rainwood.water_authority.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.recyclerview.widget.RecyclerView;

import com.rainwood.water_authority.R;
import com.rainwood.water_authority.aop.SingleClick;
import com.rainwood.water_authority.base.MyBaseActivity;
import com.rainwood.water_authority.helper.InputTextHelper;
import com.rainwood.water_authority.http.request.IssueGoodsDetailApi;
import com.rainwood.water_authority.http.request.IssueGoodsDetailByOutboundApi;
import com.rainwood.water_authority.http.request.IssueGoodsOutboundApi;
import com.rainwood.water_authority.http.response.IssueGoodsDetailBean;
import com.rainwood.water_authority.http.response.SimpleResultBean;
import com.rainwood.water_authority.model.IssueNoticeBean;
import com.rainwood.water_authority.other.IntentKey;
import com.rainwood.water_authority.ui.adapter.IssueOutboundBaseAdapter;
import com.rainwood.water_authority.ui.dialog.MessageDialog;
import com.rainwood.water_authority.util.SoftHideKeyBoardUtils;
import com.sxs.easyhttp.EasyHttp;
import com.sxs.easyhttp.listener.HttpCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sxs
 * @date :2020/9/28
 * @desc: 物资出库 -- 发料
 */
public final class IssueOutboundAty extends MyBaseActivity {

    public static void start(Context context, String goodsId, String materialId) {
        Intent intent = new Intent(context, IssueOutboundAty.class);
        intent.putExtra(IntentKey.ID, goodsId);
        intent.putExtra(IntentKey.ORDER, materialId);
        context.startActivity(intent);
    }

    private RecyclerView mOutboundListView;
    private EditText mOutboundNumView;
    private Button mConfirmBtn;

    private IssueOutboundBaseAdapter mIssueOutboundAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_issue_out_bound;
    }

    @Override
    protected void initView() {
        mOutboundListView = findViewById(R.id.rv_outbound_list);
        mOutboundNumView = findViewById(R.id.et_outbound_num);
        mConfirmBtn = findViewById(R.id.btn_confirm);

        mIssueOutboundAdapter = new IssueOutboundBaseAdapter(this);
        mOutboundListView.setAdapter(mIssueOutboundAdapter);

        InputTextHelper.with(this)
                .addView(mOutboundNumView)
                .setMain(mConfirmBtn)
                .build();
        setOnClickListener(mConfirmBtn);
        SoftHideKeyBoardUtils.assistActivity(this);
    }

    @Override
    protected void initData() {
        EasyHttp.post(this)
                .api(!TextUtils.isEmpty(getString(IntentKey.ORDER))
                        ? (new IssueGoodsDetailApi()
                        .setCsId(getString(IntentKey.ID))
                        .setNoteId(getString(IntentKey.ORDER)))
                        :(new IssueGoodsDetailByOutboundApi()
                            .setGoodsId(getString(IntentKey.ID))))
                .request(new HttpCallback<IssueGoodsDetailBean>(this) {
                    @Override
                    public void onSucceed(IssueGoodsDetailBean result) {
                        if (result.getData() != null) {
//                            if (result.getCode() == 0){
//                                toast(result.getWarn());
//                                postDelayed(() -> finish(), 1000);
//                                return;
//                            }
//                            if (!"issue".equals(result.getData().getcType())){
//                                toast("非发料物资");
//                                postDelayed(() -> finish(), 1000);
//                                return;
//                            }
                            IssueGoodsDetailBean.DataDTO resultData = result.getData();
                            List<IssueNoticeBean> issueNoticeList = new ArrayList<>();
                            IssueNoticeBean bean = new IssueNoticeBean();
                            bean.setGoodsId(resultData.getGoodsId());
                            bean.setIssueName(resultData.getName());
                            bean.setApplyNum(resultData.getApplicationsNum());
                            bean.setOutboundNum(resultData.getReceiveNum());
                            bean.setIssueNo(resultData.getNumber());
                            bean.setModel(resultData.getSpeciModel());
                            bean.setUnit(resultData.getUnit());
                            issueNoticeList.add(bean);
                            mIssueOutboundAdapter.setData(issueNoticeList);
                        }else {
                            if (result.getCode() == 0){
                                toast(result.getWarn());
                                ErrorPageAty.start(getContext(), "发料出库失败");
                                finish();
                            }
                        }
                    }
                });
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                String numStr = mOutboundNumView.getText().toString().trim();
                int num = Integer.parseInt(numStr);
                if (mIssueOutboundAdapter.getData() != null
                        && mIssueOutboundAdapter.getData().get(0).getApplyNum() < num) {
                    new MessageDialog.Builder(this)
                            .setMessage("不能超过本次申领数量")
                            .setCancel(null)
                            .setListener(dialog -> {
                                //
                            }).show();
                    return;
                }
                // 物资出库
                EasyHttp.post(this)
                        .api(new IssueGoodsOutboundApi()
                                .setGoodsId(mIssueOutboundAdapter.getData().get(0).getGoodsId())
                                .setNum(num))
                        .request(new HttpCallback<SimpleResultBean>(this){
                            @Override
                            public void onSucceed(SimpleResultBean result) {
                                toast(result.getWarn());
                                if (result.getCode() == 1){
                                    postDelayed(() -> finish(), 1500);
                                }
                            }
                        });
                break;
            default:
                break;
        }
    }
}
